import java.util.LinkedList;
import java.util.Queue;

class MyStack {
    private Queue<Integer> que1;
    private Queue<Integer> que2;

    public MyStack() {
        que1 = new LinkedList<>();
        que2 = new LinkedList<>();
    }
    
    public void push(int x) {
        if (!que1.isEmpty()) {
            que1.offer(x);
        } else if (!que2.isEmpty()) {
            que2.offer(x);
        }else {
            //都为空,默认放到第一个队列
            que1.offer(x);
        }
    }
    public int pop() {
        if(empty()){
            return -1;
        }
        if (!que1.isEmpty()) {
            int size = que1.size() - 1;
            for (int i = 0; i < size; i++) {
                Integer x = que1.poll();
                que2.offer(x);
            }
            return que1.poll();
        }else {
            int size = que2.size() - 1;
            for (int i = 0; i < size; i++) {
                Integer x = que2.poll();
                que1.offer(x);
            }
            return que2.poll();
        }
    }
    
    public int top() {
        if(empty()){
            return -1;
        }
        if (!que1.isEmpty()) {
            int size = que1.size();
            int x = 0;
            for (int i = 0; i < size; i++) {
                x = que1.poll();
                que2.offer(x);
            }
            return x;
        }else {
            int size = que2.size();
            int x = 0;
            for (int i = 0; i < size; i++) {
                x = que2.poll();
                que1.offer(x);
            }
            return x;
        }
    }
    
    public boolean empty() {
        //两个队列都为空的时候,说明我们模拟实现的栈是空的
        return que1.isEmpty() && que2.isEmpty();
    }
}